﻿
using TinyToolKit.UI;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MarketUIForm : DefaultUIFormBase
{
    public ScrollRect scrollRect;
    public int itemsCount;
    public GameObject itemPrefab;

    protected override void OnEnter()
    {
        base.OnEnter();

        if (scrollRect.content.childCount > 0)
        {
            for (int i = 0; i < scrollRect.content.childCount; i++)
            {
                Destroy(scrollRect.content.GetChild(i).gameObject); 
            }
        }

        for (int i = 0; i < itemsCount; i++)
        {
            GameObject item = GameObject.Instantiate(itemPrefab, scrollRect.content);
            item.name = "商品 " + (i + 1).ToString();
            item.GetComponentInChildren<Text>().text = item.name;
            item.GetComponent<Button>().onClick.AddListener(OnProductBtnClick);
        }
    }

    protected override void OnPause()
    {
        base.OnPause();
        
        gameObject.SetActive(true);
    }

    private void OnProductBtnClick()
    {
        ProductInfoUIForm formBase = OpenUIForm("ProductInfoUIForm") as ProductInfoUIForm;
        if (formBase != null) formBase.SetText(EventSystem.current.currentSelectedGameObject.name);
    }
}
